//
//  SynDataViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/11.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "SynDataViewController.h"
#import "ExpandDynamicCell.h"
#import "SynDataCell.h"

NSString const *SynDataViewControllerTitleParamKey = @"SynDataViewControllerTitleParamKey";

@interface SynDataViewController ()<XUIGroupTableViewDelegate,XUIGroupTableViewDataSource,ExpandDynamicCellDelegate,XUIButtonDelegate>
{
    NSString *_title;
    NSMutableArray *_qdData;
    XUIGroupTableView *_gtable;
    CGFloat _mine_backgroundHeight;
    
    XUIButton *_synDataBtn;
    RSWDataOperation *_manager;
    
    NSString *_dicStatus;
    NSString *_menuStatus;
    NSString *_userStatus;
    NSString *_deptStatus;
}
@end

@implementation SynDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    
    [self initVariable];
    [self initData];
    [self initUI];
}

- (void)initVariable
{
    _title = self.param[SynDataViewControllerTitleParamKey];
    _mine_backgroundHeight = 100 + 44;
    _qdData = [NSMutableArray new];
    _manager = [[RSWDataOperation alloc]initWithDataOperationManagerDelegate:self];
    
    _dicStatus = @"";
    _menuStatus = @"";
    _userStatus = @"";
    _deptStatus = @"";
}

- (void)initUI
{
    CGFloat top = [XUINavigationController navigationController].navigationBar.bottom;
    _gtable = [[XUIGroupTableView alloc]initWithFrame:CGRectMake(0, top, self.view.width, self.view.height)];
    _gtable.delegate = self;
    _gtable.dataSource = self;
    
    [self.view addSubview:_gtable];
}

-(void)initData
{
    [_qdData addObject:@"数据字典"];
    [_qdData addObject:@"桌面数据"];
    [_qdData addObject:@"用户缓存"];
    [_qdData addObject:@"部门缓存"];
}

#pragma mark --
#pragma mark -- groupTableView delegate

- (NSUInteger)numberOfGroupsInGroupTableView:(XUIGroupTableView *)groupTableView
{
    return 3;
}

- (NSUInteger)groupTableView:(XUIGroupTableView *)groupTableView numberOfRowsInGroup:(NSInteger)group
{
    if(group ==0)
        return 1;
    else if(group == 1)
        return _qdData.count;
    else if(group ==2)
        return 1;
    return 0;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForHeaderInGroup:(NSInteger)group
{
    return 0;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat top = [XUINavigationController navigationController].navigationBar.bottom;
    
    if(indexPath.section ==0)
        return _mine_backgroundHeight;
    else if(indexPath.section == 1)
        return 64;
    else if(indexPath.section == 2)
        return self.view.height - _mine_backgroundHeight - 64*_qdData.count - top;
    return 0;
}

- (XUIGroupTableViewCell *)groupTableView:(XUIGroupTableView *)groupTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [self groupTableView:groupTableView heightForRowAtIndexPath:indexPath];
    if(indexPath.section ==0)
    {
        XUIGroupTableViewCell *cell = [[XUIGroupTableViewCell alloc] initWithFrame:CGRectZero];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        CGFloat width = groupTableView.width - 30 - 30;
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 0,  width, 30)];
        titleLabel.font = [UIFont systemFontOfSize:30];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = _title;
        titleLabel.bottom = height - 30;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        
        cell.backgroundColor = [UIColor colorWithHex:mcrmSAASUIShinColorTextStyle2196F3Color];
        
        [cell addSubview:titleLabel];
        
        return cell;
    }
    else if(indexPath.section == 1)
    {
        SynDataCell *cell = [[SynDataCell alloc] initWithFrame:CGRectMake(10, 0, groupTableView.width, height)];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.title.text = _qdData[indexPath.row];
        
        cell.status.text = @"未开始";
        
        if(indexPath.row == 0)
        {
            if([_dicStatus isEqualToString:@""] == NO)
            {
                cell.status.text = _dicStatus;
                cell.logoUrl.image = [UIImage imageNamed:@"img_download_success"];
            }
        }
        
        if(indexPath.row == 1)
        {
            if([_menuStatus isEqualToString:@""] == NO)
            {
                cell.status.text = _menuStatus;
                cell.logoUrl.image = [UIImage imageNamed:@"img_download_success"];
            }
        }
        
        if(indexPath.row == 2)
        {
            if([_dicStatus isEqualToString:@""] == NO)
            {
                cell.status.text = _userStatus;
                cell.logoUrl.image = [UIImage imageNamed:@"img_download_success"];
            }
        }
        
        if(indexPath.row == 3)
        {
            if([_deptStatus isEqualToString:@""] == NO)
            {
                cell.status.text = _deptStatus;
                cell.logoUrl.image = [UIImage imageNamed:@"img_download_success"];
            }
        }
        
        return cell;
    }
    else if(indexPath.section == 2)
    {
        XUIGroupTableViewCell *cell = [[XUIGroupTableViewCell alloc] initWithReuseIdentifier:@"XUIGroupTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        CGFloat bottom = height -13;
        
        _synDataBtn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeCustom delegate:self];
        _synDataBtn.frame = CGRectMake(13, 0, groupTableView.width - 13 - 13, 44);
        _synDataBtn.bottom = bottom;
        _synDataBtn.backgroundColor = [UIColor colorWithHex:0x25b6ed];
        _synDataBtn.layer.cornerRadius = 2;
        _synDataBtn.layer.masksToBounds = YES;
        [_synDataBtn SetTitleColor:[UIColor whiteColor]];
        [_synDataBtn SetFont:[UIFont systemFontOfSize:18]];
        [_synDataBtn SetTitle:@"开始同步"];
        
        [cell addSubview:_synDataBtn];
        
        return cell;
    }
    
    return nil;
}

-(void)clickedUpInsideOfButton:(XUIButton *)btn
{
    if(btn == _synDataBtn)
    {
        [_synDataBtn setEnabled:NO];
        
        [_manager synDataCompletion:^(NSInteger nResult, BOOL bResult, NSError *error)
         {
             switch(nResult)
             {
                 case YES:
                 {
                     [NSThread sleepForTimeInterval:3];
                     [XUINavigationController pop];
                     
                     break;
                 }
                 default:
                 {
                     //[UIAlertView showErrorMsg:@"同步失败"];
                     [_synDataBtn setEnabled:YES];
                     break;
                 }
             }
         }];
    }
}

- (void)loginStatus:(NSString *)szStatus
{
    DispatchMainThreadAsync(^
    {
        if([szStatus rangeOfString:RSWLoginOperationLoadBaseDataDicStatusTip].location != NSNotFound)
        {
            _dicStatus = @"同步完成";
            [_gtable reloadData];
        }
        else if([szStatus rangeOfString:RSWLoginOperationLoadBaseDataMenuStatusTip].location != NSNotFound)
        {
            _menuStatus = @"同步完成";
            [_gtable reloadData];
        }
        else if([szStatus rangeOfString:RSWLoginOperationLoadUserInfoStatusTip].location != NSNotFound)
        {
            _userStatus = @"同步完成";
            [_gtable reloadData];
        }
        else if([szStatus rangeOfString:RSWLoginOperationLoadAddressBookStatusTip].location != NSNotFound)
        {
            _deptStatus = @"同步完成";
            [_gtable reloadData];
        }
    });
}

-(void)beginProgressView:(XMSDataOperationManager *)dom funcationName:(NSString *)szFuncationname
{
    if([szFuncationname isEqualToString:@"synData"])
    {
        [self beginProgressViewPanel:@"正在同步..."];
    }
    else
    {
        [super beginProgressView:dom funcationName:szFuncationname];
    }
}

-(void)endProgressView:(XMSDataOperationManager *)dom funcationName:(NSString *)szFuncationname
{
    if([szFuncationname isEqualToString:@"synData"])
    {
        [self beginProgressViewPanel:@"同步完成..."];
    }
    else
    {
        [super beginProgressView:dom funcationName:szFuncationname];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
